
#pragma once
#ifndef _NOSERVER_

#include "ListenSocket.h"

enum StopThread_Reason;

class CServerSocket;
class CClientThread;
class IServerSocketDelegate
{
public:
	virtual void ClientStarted(CClientThread *pCltThread) = 0;
	virtual void ClientStoped(CClientThread *pCltThread, StopThread_Reason reason) = 0;
	virtual void ClientDataRecved(CClientThread *pCltThread, byte * data, intptr_t dataLength) = 0;
	virtual void ClientThreadIdle(CClientThread *pCltThread) = 0;
	virtual void ServerThreadStarted(CServerSocket *socket) = 0;
	virtual void ServerThreadIdle(CServerSocket *socket) = 0;
	virtual void ServerThreadStoped(CServerSocket *socket) = 0;
};

class CServerSocket : public CListenSocket
{
public:
	CServerSocket(IServerSocketDelegate *delegate);
	~CServerSocket() override;

	CClientThread *GetClientThread(intptr_t idx);
	IServerSocketDelegate *GetDelegate(){ return m_delegate; }
protected:
	LPVOID initClientThread() override;
	void doneClientThread() override;
	bool startClientThread(SOCKET cltSock) override;
	void procStarted() override { m_delegate->ServerThreadStarted(this); }
	void procIdle() override { m_delegate->ServerThreadIdle(this); }
	void procFinished() override { m_delegate->ServerThreadStoped(this); }

	IServerSocketDelegate *m_delegate;
};

#endif
